using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using InfoJetSoft.Service.Entity;

namespace InfoJetSoft.Service.Util
{

	/// <summary>
	/// Summary description for ExtMsxslFunction.
	/// </summary>
    public class ExtMsxslFunction : IExtensionFunctions
	{
        private Form form = null;

		public ExtMsxslFunction( Form frm )
		{
            this.form = frm;
        }

        //CID 0755 //2011.01.14 //Begin
        #region IExtensionFunction Members

        public string Prefix
        {
            get { return this.form.Manifest.NsResolver.GetPrefix(this.NamespaceURI); }
        }

        public string NamespaceURI
        {
            get { return JetConstant.URI_XDMSXSL; }
        }

        public IXsltContextFunction ResolveFunction(string prefix, string name, XPathResultType[] argTypes)
        {
            //CID 0931 //2011.05.29 //Begin
            if (name.Equals("string_compare"))
            {
                return new XPathFunction(this.form, prefix, name, 2, 2, argTypes, XPathResultType.Number);
            }
            else
            {
                throw new InfoJetException("Unimplemented Function.", prefix + ":" + name);
            }
            //CID 0931 //2011.05.29 //End
        }

        public object InvokeFunction(string prefix, string name, XsltContext xsltContext, object[] args, XPathNavigator docContext)
        {
            //CID 0931 //2011.05.29 //Begin
            if (name.Equals("string_compare"))
            {
                return this.string_compare(args[0], args[1]);
            }
            else
            {
                throw new InfoJetException("Unimplemented Function.", prefix + ":" + name);
            }
            //CID 0931 //2011.05.29 //End
        }

        #endregion
        //CID 0755 //2011.01.14 //End

        public int string_compare(object x, object y)
		{
            string xStr = convert2String(x);
            string yStr = convert2String(y);
			return xStr.CompareTo( yStr );
		}

        //CID 0931 //2011.05.29 //Begin
        private string convert2String(object obj)
        {
            if (obj is XPathNodeIterator)
            {
                XPathNodeIterator nodeIterator = (XPathNodeIterator)obj;
                if (nodeIterator.MoveNext())
                {
                    return nodeIterator.Current.Value;
                }
                else
                {
                    return "";
                }
            }
            else if (obj is double)
            {
                double doubleValue = (double)obj;
                string strValue = doubleValue.ToString("R", System.Globalization.NumberFormatInfo.InvariantInfo);
                return strValue;
            }
            else if (obj is bool)
            {
                bool boolValue = (bool)obj;
                if (boolValue)
                {
                    return "true";
                }
                else
                {
                    return "false";
                }
            }
            else if (obj is string)
            {
                return (string)obj;
            }
            else
            {
                return obj.ToString();
            }
        }
        //CID 0931 //2011.05.29 //End
    }
}
